package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.Fenlei;
import com.ruoyi.system.mapper.ShangpinMapper;
import com.ruoyi.system.domain.Shangpin;
import com.ruoyi.system.service.IShangpinService;

/**
 * 商品列表Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-10
 */
@Service
public class ShangpinServiceImpl implements IShangpinService 
{
    @Autowired
    private ShangpinMapper shangpinMapper;

    /**
     * 查询商品列表
     * 
     * @param spId 商品列表主键
     * @return 商品列表
     */
    @Override
    public Shangpin selectShangpinBySpId(Long spId)
    {
        return shangpinMapper.selectShangpinBySpId(spId);
    }

    /**
     * 查询商品列表列表
     * 
     * @param shangpin 商品列表
     * @return 商品列表
     */
    @Override
    public List<Shangpin> selectShangpinList(Shangpin shangpin)
    {
        return shangpinMapper.selectShangpinList(shangpin);
    }
    //多表查询
    @Override
    public List<Shangpin> selectListAll(Shangpin shangpin) {
        return shangpinMapper.selectListAll(shangpin);
    }

    /**
     * 新增商品列表
     * 
     * @param shangpin 商品列表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertShangpin(Shangpin shangpin)
    {
        int rows = shangpinMapper.insertShangpin(shangpin);
        insertFenlei(shangpin);
        return rows;
    }

    /**
     * 修改商品列表
     * 
     * @param shangpin 商品列表
     * @return 结果
     */
    @Transactional
    @Override
    public int updateShangpin(Shangpin shangpin)
    {
        shangpinMapper.deleteFenleiByFlId(shangpin.getSpId());
        insertFenlei(shangpin);
        return shangpinMapper.updateShangpin(shangpin);
    }

    /**
     * 批量删除商品列表
     * 
     * @param spIds 需要删除的商品列表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteShangpinBySpIds(Long[] spIds)
    {
        shangpinMapper.deleteFenleiByFlIds(spIds);
        return shangpinMapper.deleteShangpinBySpIds(spIds);
    }

    /**
     * 删除商品列表信息
     * 
     * @param spId 商品列表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteShangpinBySpId(Long spId)
    {
        shangpinMapper.deleteFenleiByFlId(spId);
        return shangpinMapper.deleteShangpinBySpId(spId);
    }

    /**
     * 新增商品分类信息
     * 
     * @param shangpin 商品列表对象
     */
    public void insertFenlei(Shangpin shangpin)
    {
        List<Fenlei> fenleiList = shangpin.getFenleiList();
        Long spId = shangpin.getSpId();
        if (StringUtils.isNotNull(fenleiList))
        {
            List<Fenlei> list = new ArrayList<Fenlei>();
            for (Fenlei fenlei : fenleiList)
            {
                fenlei.setFlId(spId);
                list.add(fenlei);
            }
            if (list.size() > 0)
            {
                shangpinMapper.batchFenlei(list);
            }
        }
    }
}
